package blyth;

public class WorldIterator {

    private World world;
    // x,y : current position, cx, cy: center position
    public int x, y;
    private int range, type, cx, cy;
    private boolean includeMiddle, isNotEnd;

    // Type: 0 square-like, 1 circle-like
    public WorldIterator(World world, int cx, int cy, int range, boolean includeMiddle, int type) {
        this.world = world;
        this.cx = cx;
        this.cy = cy;
        this.range = range;
        this.includeMiddle = includeMiddle;
        this.type = type;
        this.isNotEnd = true;
        this.y = cy - range;
        if (type == 0) {
            this.x = cx - range;
        } else if (type == 1) {
            this.x = cx;
        }
        if (World.IsValidTile(x,y) == false) {
            Next();
        }
    }

    public void Next() {
        if (isNotEnd == false) {
            return;
        }
        if (type == 0) {
            if (x < cx + range) {
                x++;
            } else {
                y++;
                x = cx - range;
            }
        } else if (type == 1) {
            if (x < cx + range - Math.abs(y - cy)) {
                x++;
            } else {
                y++;
                x = cx - range + Math.abs(y - cy);
            }
        }
        if (includeMiddle == false && cx == x && cy == y) {
            x++;
        }
        if (y > cy + range) {
            isNotEnd = false;
        } else if (World.IsValidTile(x,y) == false) {
            Next();
        }
    }

    public boolean IsNotEnd() {
        return isNotEnd;
    }    

    public Tile Get() {
        return world.tile[x][y];
    }
    
    public int Distance() {
        return Heur.Manhattan(x, y, cx, cy);
    }
}
